package vn.edu.saigontech.ncms.web.system;

import java.util.Collection;

import vn.edu.saigontech.ncms.model.system.Bank;
import vn.edu.saigontech.ncms.model.system.BankAccount;
import vn.edu.saigontech.ncms.service.BankAccountService;
import vn.edu.saigontech.ncms.service.BankService;
import vn.edu.saigontech.ncms.service.impl.BankServiceImpl;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class BankAction extends BaseAction {
	private Long id;
	private Long bankId;
	private String name;
	private String number;
	private boolean active;

	public String list() {
		Collection<Bank> banks = BankService.INSTANCE.findAll();
		REQUEST().setAttribute("banks", banks);
		return SUCCESS;
	}
	
	public String showAdd() {
		return SUCCESS;
	}
	
	public String add() {
		Bank bank = new Bank();
		bank.setName(this.name);
		bank = BankService.INSTANCE.persist(bank);
		if(Checker.isNotNull(bank)) {
			return SUCCESS;
		}
		return ERROR;
	}
	
	public String showEdit() {
		Bank bank = BankService.INSTANCE.findById(this.id);
		REQUEST().setAttribute("bank", bank);
		return SUCCESS;
	}
	
	public String edit() {
		Bank bank = BankService.INSTANCE.findById(this.id);
		bank.setName(this.name);
		if(Checker.isNotNull(bank)) {
			return SUCCESS;
		}
		return ERROR;
	}
	
	public String delete() {
		if(BankService.INSTANCE.deleteById(this.id)) {
			return SUCCESS;
		}
		return ERROR;
	}
	
	public String listAcc() {
		Collection<BankAccount> bankAccounts = BankAccountService.INSTANCE.findAll();
		REQUEST().setAttribute("bankAccounts", bankAccounts);
		return SUCCESS;
	}
	
	public String showAddAcc() {
		Collection<Bank> banks = BankService.INSTANCE.findAll();
		REQUEST().setAttribute("banks", banks);
		return SUCCESS;
	}
	
	public String addAcc() {
		BankAccount bankAccount = new BankAccount();
		bankAccount.setNumber(this.number);
		bankAccount.setActive(true);
		bankAccount.setBank(BankServiceImpl.INSTANCE.findById(this.bankId));
		bankAccount = BankAccountService.INSTANCE.persist(bankAccount);
		if(Checker.isNotNull(bankAccount)) {
			return SUCCESS;
		}
		return ERROR;
	}
	
	public String showEditAcc() {
		BankAccount bankAccount = BankAccountService.INSTANCE.findById(this.id);
		REQUEST().setAttribute("bankAccount", bankAccount);
		Collection<Bank> banks = BankService.INSTANCE.findAll();
		REQUEST().setAttribute("banks", banks);
		return SUCCESS;
	}
	
	public String editAcc() {
		BankAccount bankAccount = BankAccountService.INSTANCE.findById(this.id);
		bankAccount.setNumber(this.number);
		bankAccount.setActive(this.active);
		bankAccount.setBank(BankServiceImpl.INSTANCE.findById(this.bankId));
		bankAccount = BankAccountService.INSTANCE.persist(bankAccount);
		if(Checker.isNotNull(bankAccount)) {
			return SUCCESS;
		}
		return ERROR;
	}
	
	public String deleteAcc() {
		if(BankAccountService.INSTANCE.deleteById(this.id)) {
			return SUCCESS;
		}
		return ERROR;
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNotation() {
		return number;
	}

	public void setNotation(String notation) {
		this.number = notation;
	}
	
	public Long getBankId() {
		return bankId;
	}

	public void setBankId(Long bankId) {
		this.bankId = bankId;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}
}